package com.example.meeting.mapper;

import com.example.meeting.model.domain.Person;
import com.example.meeting.model.domain.Subject;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SubjectMapper {
    //查询议题
    Subject selectSubjectById(Long id);

    //动态查询议题列表
    List<Subject> selectSubjectList(Subject subject);

    //根据会议Id查询议题列表
    @Select("SELECT * FROM subject where m_id = #{meetingId} ORDER BY weight DESC")
    @ResultMap("subjectWithPersons")
    List<Subject> selectSubjectListByMeetingId(Long meetingId);

    //新增议题
    @Insert("INSERT INTO subject(name,weight,status,m_id) " +
            " VALUES(#{name}, ( select IFNULL(MIN(s.weight), 114515) from subject s where s.m_id = #{meeting.id} ) - 1,#{status},#{meeting.id})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    Integer addSubject(Subject subject);

    //更新议题
    Integer updateSubject(Subject subject);

    //更新议题状态
    @Update("UPDATE subject SET status=#{status} WHERE id=#{id}")
    Integer updateSubjectStatus(Subject subject);

    //批量更新议题权重
    Integer updateSubjectWeightBatch(List<Subject> subjectList);

    //删除议题
    @Delete("DELETE FROM subject where id=#{id}")
    Integer deleteSubject(Long id);

    //添加议题成员id记录
    Integer addSubjectPerson(Long subjectId,@Param("list") List<Person> personList);

    //删除议题成员id记录
    @Delete("DELETE FROM subject_person WHERE s_id=#{subjectId}")
    Integer deleteSubjectPerson(Long subjectId);

    //根据会议id删除所属议题
    @Delete("DELETE FROM subject WHERE m_id=#{mId}")
    Integer deleteByMId(Long mId);

    //根据议题id和人员id删除记录
    @Delete("DELETE FROM subject_person WHERE s_id=#{sId} AND p_id=#{pId}")
    Integer deletePersonAndSub(Integer sId, Integer pId);
}
